package cn.wp.common.core.generate;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.wp.common.core.generate.meta.Author;
import cn.wp.common.core.generate.meta.Table;

import java.util.Scanner;

/**
 * 代码生成运行类
 * Created by chenyb on 2019/10/22
 */
public class GenerateCodeRunner {

    // 运行
    public static void run() {
        Scanner scanner = new Scanner(System.in);
        // 解决回车及空格输入问题
        scanner.useDelimiter("\n");
        Console.log("请选择需要生成的代码");
        Console.log("0-Sql格式化");
        Console.log("1-Do");
        Console.log("2-Dto");
        Console.log("3-Dao");
        Console.log("4-Service");
        Console.log("5-Controller");
        Console.log("6-以上全部");

        Console.log("请选择：");

        String sql = "";
        GenerateCode generateCode = null;
        String tableCode = "";
        String tableRemark = "";
        String authorName = "";
        int step = 0;
        while(scanner.hasNext()){
            String line = scanner.nextLine();
            if(StrUtil.isBlank(line)){
                throw new IllegalArgumentException("输入不能为空");
            }

            // sql 格式化操作
            if("0".equals(line)){
                SqlFormat.run();
                break;
            }

            step++;
            switch (line){
                case "1":
                    generateCode = new GenerateDoTemplate();
                    break;
                case "2":
                    generateCode = new GenerateDtoTemplate();
                    break;
                case "3":
                    generateCode = new GenerateDaoTemplate();
                    break;
                case "4":
                    generateCode = new GenerateServiceTemplate();
                    break;
                case "5":
                    generateCode = new GenerateControllerTemplate();
                    break;
                case "6":
                    generateCode = new GenerateAllTemplate();
                    break;
                default:
                    break;
            }

            if(step == 1){
                Console.log("请输入表名:");
            }

            if(step == 2){
                tableCode = line;
                if(StrUtil.isBlank(tableCode)){
                    throw new IllegalArgumentException("表名不能为空");
                }
                Console.log("请输入表备注信息:");
            }

            if(step == 3){
                tableRemark = line;
                if(StrUtil.isBlank(tableCode)){
                    throw new IllegalArgumentException("备注不能为空");
                }
                Console.log("请输入作者名称:");
            }

            if(step == 4){
                authorName = line;
                if(StrUtil.isBlank(authorName)){
                    throw new IllegalArgumentException("作者不能为空");
                }
                Table table = new Table();
                table.setCode(tableCode);
                table.setName(tableRemark);

                Author author = new Author();
                author.setName(authorName);

                Console.log("开始生成......");
                String codeStr = generateCode.genCode(table,author);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.err.println("------------------------------------------------------");
                System.err.println(codeStr);
                System.err.println("------------------------------------------------------");
                break;
            }
        }

    }

    public static void main(String[] args) {
        GenerateCodeRunner.run();
    }

}
